package com.yiren.dbaa.module.practice_set.line.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.utils.GpsUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.global.GlobalApplication;
import com.yiren.dbaa.view.dialog.CustomDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/3/22 20:19
 * @description: 线路view的加载控制器
 */
public class LineViewLoadDialog {
    /**
     * 登录对话框
     */
    public void toLoginDialogLine(Activity activity) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setMessage("需要先登录才能下载线路，是否现在登录？");
        builder.setTitle("提示");
        builder.setPositiveButton("登录", (dialog, which) -> {
            GlobalApplication.getInstance().logOut(activity);
            dialog.dismiss();
        });
        builder.setNegativeButton("取消", (dialog, which) -> {
            dialog.dismiss();
        });
        builder.create().show();
    }

    /**
     * 线路未保存对话框
     */
    public void noSaveLine(Activity activity) {
        CustomDialog.Builder builder = new CustomDialog.Builder(activity);
        builder.setTitle("温馨提示");
        builder.setMessage("您采集的点位还未保存，是否放弃保存？");
        builder.setCanceled(false);
        builder.setPositiveButton("继续编辑", (dialog, which) -> dialog.dismiss());
        builder.setNegativeButton("放弃保存", (dialog, which) -> {
            dialog.dismiss();
            activity.finish();
        });
        builder.create().show();
    }

    /**
     * 点位移动对话框
     *
     * @param tzType    1：前进  2：后退
     * @param linePoint
     * @param position
     */
    public void numShowDialog(Activity activity, int tzType, LinePointBean linePoint, int position) {
        double distance = 2;  // 每次都要初始化赋值
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_line_point_move, null);
        EditText edit_box = (EditText) view.findViewById(R.id.edit_box);
        EditText etContent = (EditText) view.findViewById(R.id.edit_box1);
        etContent.setInputType(InputType.TYPE_CLASS_NUMBER);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tvSplitLine = (TextView) view.findViewById(R.id.tv_split_line);
        TextView tvDialogUnit = (TextView) view.findViewById(R.id.tv_dialog_unit);
        tvSplitLine.setVisibility(View.GONE);
        edit_box.setVisibility(View.GONE);
        etContent.setVisibility(View.VISIBLE);
        tvDialogUnit.setVisibility(View.VISIBLE);
        tvTitle.setText(tzType == 1 ? "点位前进" : "点位后退");
        Dialog dialog = new Dialog(activity, R.style.MyDialog);
        dialog.show();
        dialog.getWindow().setGravity(Gravity.CENTER);
        dialog.getWindow().setContentView(view);
        dialog.setCancelable(false);
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        dialog.getWindow().setLayout((int) (width * 0.7), ViewGroup.LayoutParams.WRAP_CONTENT);
        etContent.setText(distance + "");
        // 设置光标位置在文字后面
        etContent.setSelection(String.valueOf(distance).length());
        /** 3.自动弹出软键盘 **/
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(etContent, InputMethodManager.SHOW_IMPLICIT);
            }
        });
        view.findViewById(R.id.tv_ok).setOnClickListener(v -> {
            String summary = etContent.getText().toString();
            if (!StringUtils.isEmpty(summary)) {
                try {
                    double sum = Double.parseDouble(summary);
                    if (sum > 30) {
                        MessageBox.show(activity, "请输入0到30之间的数字");
                        return;
                    }
                    GpsInfoBean gpsInfo;
                    if (tzType == 1) {  //前进
                        gpsInfo = GpsUtil.getMyLatLng(linePoint.getLongitude(), linePoint.getLatitude(), sum / 1000, linePoint.getBearing());
                    } else {   //后退
                        gpsInfo = GpsUtil.getMyLatLng(linePoint.getLongitude(), linePoint.getLatitude(), -(sum / 1000), linePoint.getBearing());
                    }
                    // 修改点位
                    linePoint.setLongitude(gpsInfo.getLongitude());
                    linePoint.setLatitude(gpsInfo.getLatitude());
                    // 更新点位信息，轨迹刷新
//                    pointList.set(position, linePoint);
                    if (tzType == 1) {
                        MessageBox.show(activity, linePoint.getProjectInfo().getName() + "前进" + sum + "米");
                    } else {
                        MessageBox.show(activity, linePoint.getProjectInfo().getName() + "后退" + sum + "米");
                    }
                    if (onDialogClickListener != null) {
                        onDialogClickListener.OnSureClickListener();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                MessageBox.show(activity, "请输入0到30之间的数字");
            }
            dialog.dismiss();
        });
        view.findViewById(R.id.tv_cancel).setOnClickListener(v -> dialog.dismiss());
    }


    private OnDialogClickListener onDialogClickListener;

    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

    public interface OnDialogClickListener {

        /**
         * 确定点击回调
         */
        void OnSureClickListener();

    }

}